/**
 * CircleMemberListActivity.java
 * com.jamin.aabill.ui
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-28 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.ui;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jamin.aabill.R;
import com.jamin.aabill.dao.CircleMemberDao;
import com.jamin.aabill.data.CircleMember;
import com.jamin.aabill.utils.AppConstant;
import com.jamin.aabill.utils.TimeUtils;
import com.jamin.aabill.utils.Utils;

/**
 * ClassName:CircleMemberListActivity
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-28 上午10:40:42 
 */
public class CircleMemberListActivity extends Activity{

	private static final String TAG = "CircleMemberListActivity ====wangjieming ";

	BroadcastReceiver receiver = null;

	ListView listView = null;
	CircleMemberAdapter mMemberAdapter = null;
	List<CircleMember> circlememberlist = null;
	long circleId = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.circle_member_list_activity);
		circleId = getIntent().getLongExtra("circleId", -1);
		initView();
		refreshView();
		receiver = new RefreshBroadcastReceiver();
		registerReceiver(receiver, new IntentFilter(AppConstant.BILL_DETAIL_REFRESH_ACTION));
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver);
		Utils.log(TAG, "CircleBillListActivity=========onDestroy");
	}

	private void initView() {
		listView = (ListView) findViewById(R.id.circle_member_list_view);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
				Intent intent = new Intent(CircleMemberListActivity.this , MemberBillDetailActivity.class);
				intent.putExtra("circleMemberId", circlememberlist.get(position).get_id());
				startActivity(intent);
			}
		});

	}

	private void refreshView() {
		initDate();
		mMemberAdapter = new CircleMemberAdapter(this, circlememberlist);
		listView.setAdapter(mMemberAdapter);
	}


	private void initDate() {
		CircleMemberDao mDao = new CircleMemberDao();
		circlememberlist = mDao.getCircleMemberList(circleId);
	}


	class CircleMemberAdapter extends BaseAdapter{

		
		List<CircleMember> circleMemberList = null;
		LayoutInflater inflater = null;


		public CircleMemberAdapter(Context ctx , List<CircleMember> list){
			this.circleMemberList = list;
			inflater = LayoutInflater.from(ctx);
		}


		@Override
		public int getCount() {
			return circleMemberList.size();
		}

		@Override
		public Object getItem(int position) {
			return circleMemberList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return circleMemberList.get(position).get_id();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.circle_member_list_item, null);
				viewHolder.mNameTextView = (TextView) convertView.findViewById(R.id.circle_member_name);
				viewHolder.mDescriptionTextView = (TextView) convertView.findViewById(R.id.circle_member_cost);
				viewHolder.mCreateTimeTextView = (TextView) convertView.findViewById(R.id.circle_member_create_time);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.mNameTextView.setText(circleMemberList.get(position).getName());
			viewHolder.mDescriptionTextView.setText(AABillApplication.mApplicationContext.getString( R.string.circle_bill_cost , AABillApplication.mBillManager.getMemberTotalCost(circleMemberList.get(position).get_id())));
			viewHolder.mCreateTimeTextView.setText(TimeUtils.FormatTimeForm(circleMemberList.get(position).getCreate_at()));
			return convertView;
		}

		class ViewHolder{

			TextView mNameTextView = null;
			TextView mDescriptionTextView = null;
			TextView mCreateTimeTextView = null;

		}


	}


	class RefreshBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context ctx, Intent intent) {
			if(intent.getAction().equals(AppConstant.BILL_DETAIL_REFRESH_ACTION)){
				refreshView();
			}
		}

	}


}

